package com.h2n.utils;

import java.util.Date;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.text.NumberFormat;
import java.security.MessageDigest;
import java.io.ByteArrayOutputStream;

/**
 * Created by IntelliJ IDEA.
 * User: NhatNV
 * Date: Jul 30, 2009
 * Time: 2:10:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class Convert{
  
  public static String dateToString(Date _date, EDatetimeFormat _formatString){
    Format f = new SimpleDateFormat(EDatetimeFormat.getFormatString(_formatString));

    return f.format(_date);
  }

  public static Date stringToDate(String _date, EDatetimeFormat _formatString) throws ParseException {
    SimpleDateFormat cv = new java.text.SimpleDateFormat(EDatetimeFormat.getFormatString(_formatString));

    return cv.parse(_date);
  }

  public static String moneyToString(long number){
    NumberFormat currency = NumberFormat.getCurrencyInstance();

    return currency.format(number).replace("$","");
  }

  public static String moneyToString(double number){
    NumberFormat currency = NumberFormat.getCurrencyInstance();

    return currency.format(number).replace("$","");
  }

  public static String moneyToString(long number, ECurrencyFormat type){
    NumberFormat currency = NumberFormat.getCurrencyInstance();

    return currency.format(number).replace("$","") +" "+ type.toString();
  }

  public static String moneyToString(double number, ECurrencyFormat type){
    NumberFormat currency = NumberFormat.getCurrencyInstance();

    return currency.format(number).replace("$","") +" "+ type.toString();
  }

  private static byte[] hexArray = new byte[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

  public static byte[] byte2Hex(byte[] input) {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    for (int i = 0; i < input.length; i++) {
      bOut.write(hexArray[input[i] >>> 4 & 0x0F]);
      bOut.write(hexArray[input[i] & 0x0F]);
    }
    return bOut.toByteArray();
  }

  public static String byte2HexString(byte[] input) {
    return new String(byte2Hex(input));
  }

  /**
   * decode the Hex encoded byte data writing it to the given output stream,
   * whitespace characters will be ignored.
   *
   * @return the number of bytes produced.
   */
  private static byte[] decodeHexArray = new byte[103];

  static {
    for (int i = 0; i < hexArray.length; i++) {
      decodeHexArray[hexArray[i]] = (byte) i;
    }
    decodeHexArray['a'] = decodeHexArray['A'];
    decodeHexArray['b'] = decodeHexArray['B'];
    decodeHexArray['c'] = decodeHexArray['C'];
    decodeHexArray['d'] = decodeHexArray['D'];
    decodeHexArray['e'] = decodeHexArray['E'];
    decodeHexArray['f'] = decodeHexArray['F'];
  }

  public static byte[] decodeHexa(byte[] data) throws Exception {
    if (data == null) {
      return null;
    }
    if (data.length % 2 != 0) {
      throw new Exception("Invalid data length:" + data.length);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte b1, b2;
    int i = 0;
    while (i < data.length) {
      b1 = decodeHexArray[data[i++]];
      b2 = decodeHexArray[data[i++]];
      out.write((b1 << 4) | b2);
    }
    out.flush();
    out.close();
    return out.toByteArray();
  }

  public static byte[] md5(byte[] value) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(value);
    return md.digest();
  }
}
